﻿#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

//1.创建主席类；需求：单例模式，为了创建类中的对象，并且保证只有一个对象实例；
class Chairman
{
private:
	Chairman()
	{
		//cout << "---------------------创建国家主席！------------------" << endl;
	}
	//4.拷贝构造也进行私有化；
	Chairman(const Chairman& c) {}

public:
	//3.提供get方法，访问主席；
	static Chairman* getInstance()
	{
		return singletonMan;
	}
private:
	static Chairman* singletonMan;
};
Chairman* Chairman::singletonMan = new Chairman;
void testSingleton()
{
	/*Chairman c1;
	Chairman* c2 = new Chairman;
	Chairman* c3 = new Chairman;*/

	/*Chairman*cm= Chairman::singletonMan;
	Chairman*cm2= Chairman::singletonMan;*/

	//Chairman::singletonMan = NULL;

	Chairman* cm3 = Chairman::getInstance();
	//Chairman::getInstance() = NULL;
	Chairman* cm4 = Chairman::getInstance();
	if (cm3 == cm4)
	{
		cout << "cm3和cm4相同！" << endl;//cm3和cm4相同！
	}
	else
	{
		cout << "cm3和cm4不相同！" << endl;
	}

	//Chairman* cm5 = new Chairman(*cm4);
	/*if (cm5 == cm4)
	{
		cout << "cm5和cm4相同！" << endl;
	}
	else
	{
		cout << "cm5和cm4不相同！" << endl;//cm5和cm4不相同！
	}*/
}

//main267：单例模式案例创建国家主席
int main267()
{
	//2.主席的创建先于main函数调用，static在编译阶段就已经调用了；
	cout << "main函数调用！" << endl;
	testSingleton();

	/*
	---------------------创建国家主席！------------------
	main函数调用！
	cm3和cm4相同！
	*/

	system("pause");
	return EXIT_SUCCESS;
}